/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Fases;

import Negocio.NegocioBode;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/**
 *
 * @author Marcio
 */
public abstract class Fase extends GameCanvas{

    protected TiledLayer cenario;
    protected TiledLayer colisao;
    protected LayerManager layerManager;
    public static final int MIN_BUFFER = 20;
    protected int viewPortX = 0;
    protected int viewPortY = 0;
    protected boolean interrupted = false;
    protected NegocioBode bode;

    public Fase() {
        super(true);
        this.layerManager = new LayerManager();
        this.bode = new NegocioBode();
        
    }

    public NegocioBode getBode() {
        return bode;
    }

    public void setBode(NegocioBode bode) {
        this.bode = bode;
    }

    public TiledLayer getCenario() {
        return cenario;
    }

    public LayerManager getLayerManager() {
        return layerManager;
    }

    public int getViewPortX() {
        return viewPortX;
    }

    public void setViewPortX(int viewPortX) {
        this.viewPortX = viewPortX;
    }

    public int getViewPortY() {
        return viewPortY;
    }

    public void setViewPortY(int viewPortY) {
        this.viewPortY = viewPortY;
    }

    public void movimentacaoBode() {
        int keyState = this.getKeyStates();
        if ((keyState & RIGHT_PRESSED) != 0) {
            this.bode.andar(NegocioBode.DIREITA, this.cenario,this.colisao);
            //attempt to adjust the viewport to keep the sprite on the screen
            this.adjustViewport(this.bode.getSpriteBode(), this.bode.getLastDirection(), this.viewPortX + NegocioBode.SPEED, this.viewPortY);
            System.out.println("Andando pra direitaaaaaa");
        } else if ((keyState & LEFT_PRESSED) != 0) {
            this.bode.andar(NegocioBode.ESQUERDA, this.cenario,this.colisao);
            this.adjustViewport(this.bode.getSpriteBode(), this.bode.getLastDirection(), this.viewPortX - NegocioBode.SPEED, this.viewPortY);
        } else if ((keyState & UP_PRESSED) != 0) {
            this.bode.andar(NegocioBode.CIMA, this.cenario,this.colisao);
            this.adjustViewport(this.bode.getSpriteBode(), this.bode.getLastDirection(), this.viewPortX, this.viewPortY - NegocioBode.SPEED);
        } else if ((keyState & DOWN_PRESSED) != 0) {
            this.bode.andar(NegocioBode.BAIXO, this.cenario,this.colisao);
            this.adjustViewport(this.bode.getSpriteBode(), this.bode.getLastDirection(), this.viewPortX, this.viewPortY + NegocioBode.SPEED);
        } else {
            this.bode.parar();
        }

    }

    public abstract void jogar();

    public void adjustViewport(Sprite sprite, int lastDirection, int newViewPortX, int newViewPortY) {
        int sx = sprite.getX();
        int sy = sprite.getY();

        int xmin = this.viewPortX + MIN_BUFFER;
        int xmax = this.viewPortX + this.getWidth() - sprite.getWidth() - MIN_BUFFER;
        int ymin = this.viewPortY + MIN_BUFFER;
        int ymax = this.viewPortY + this.getHeight() - sprite.getHeight() - MIN_BUFFER;

        //if the sprite is not near the any screen edges don't adjust
        if (sx >= xmin && sx <= xmax && sy >= ymin && sy <= ymax) {
            return;
        }

        //if the sprite is moving left but isn't near the left edge of the screen don't adjust

        if (lastDirection == GameCanvas.LEFT && sx >= xmin) {
            return;
        }
        //if the sprite is moving right but isn't near the right edge of the screen don't adjust
        if (lastDirection == GameCanvas.RIGHT && sx <= xmax) {
            return;
        }
        //if the sprite is moving up but isn't at near top edge of the screen don't adjust
        if (lastDirection == GameCanvas.UP && sy >= ymin) {
            return;
        }
        //if the sprite is moving down but isn't at near bottom edge of the screen don't adjust
        if (lastDirection == GameCanvas.DOWN && sy <= ymax) {
            return;
        }

        //only adjust x to values that ensure the base tiled layer remains visible
        //and no white space is shown
        if (newViewPortX < cenario.getX()) {
            this.setViewPortX(cenario.getX());
        } else if (newViewPortX > cenario.getX() + cenario.getWidth() - this.getWidth()) {
            this.setViewPortX(cenario.getX() + cenario.getWidth() - this.getWidth());
        } else {
            this.setViewPortX(newViewPortX);
        }

        //only adjust y to values that ensure the base tiled layer remains visible
        //and no white space is shown
        if (newViewPortY < cenario.getY()) {
            this.setViewPortY(cenario.getY());
        } else if (newViewPortY > cenario.getY() + cenario.getHeight() - this.getHeight()) {
            this.setViewPortY(cenario.getY() + cenario.getHeight() - this.getHeight());
        } else {
            this.setViewPortY(newViewPortY);
        }

        //adjust the viewport
        this.getLayerManager().setViewWindow(this.getViewPortX(), this.getViewPortY(), this.getWidth(), this.getHeight());

    }
}
